from .searchwin import SearchWin
from .itemswin import ItemsWin
from .menuwin import MenuWin

from .constants import *

import curses

class MainWindow:

    def __init__(self, menu_items: list=[], content_items: list=[]):
        self.screen = None
        self.windows = []
        self.current_win = None
        self.menu_items = menu_items
        self.content_items = content_items

    def set_current_win(self, window):
        for win in self.windows:
            win.set_uncurrent()
        self.current_win = window
        window.set_current()

    def get_current_win_index(self):
        for index, win in enumerate(self.windows):
            if win == self.current_win:
                return index
        return -1

    def toggle_current_win(self):
        index = self.get_current_win_index()
        if index == -1: return

        index += 1
        if index >= len(self.windows): index = 0

        self.set_current_win(self.windows[index])

    def clear_windows(self):
        for win in self.windows:
            win.clear()

    def refresh_windows(self):
        for win in self.windows:
            win.refresh()

    def draw_windows(self):
        for win in self.windows:
            win.draw()

    def draw(self):
        self.clear_windows()
        self.draw_windows()        
        self.refresh_windows()

        self.screen.refresh()

    def loop(self):
        while True:
            self.draw()

            c = self.current_win.getch()
            
            if c in KEYS_ENTER:
                if type(self.current_win) == SearchWin:
                    pass # replace with search request
                else:
                    return self.current_win.get_current_item()
            elif c in KEYS_CHANGE_TAB:
                self.toggle_current_win()

            elif c in KEYS_UP:
                if type(self.current_win) != SearchWin:
                    self.current_win.move_up()
            
            elif c in KEYS_DOWN:
                if type(self.current_win) != SearchWin:
                    self.current_win.move_down()
            
            elif c in KEYS_Q:
                exit(0)


    def init_windows(self):
        max_y, max_x = self.screen.getmaxyx()
        
        search_win = SearchWin(self.screen)
        items_win = ItemsWin(self.screen, items=self.content_items)
        menu_win = MenuWin(self.screen, items=self.menu_items)

        self.set_current_win(search_win)

        self.windows.append(search_win)
        self.windows.append(menu_win)
        self.windows.append(items_win)

    def init_curses(self):
        # use the default colors of the terminal
        curses.use_default_colors()
        # hide the cursor
        curses.curs_set(0)

        curses.initscr()
        self.init_windows()

    def _start(self, screen):
        self.screen = screen
        
        self.init_curses()
        screen.idlok(False)
        screen.idcok(False)

        return self.loop()

    def start(self):
        return curses.wrapper(self._start)





